package com.tchy.cloud.tools.sms;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SendSMSForNetease {

    private static final String SERVER_URL="https://api.netease.im/sms/sendtemplate.action";//请求的URL
    private static final String APP_KEY="acb37a9ddaa652931ecdf6d2c2ac766a";//账号
    private static final String APP_SECRET="c427005ab051";//密码
    public static final String MOULD_ID="3031807";//注册模板ID
    private static final String NONCE="123456";//随机数

    /**
     * 网易云信短信发送方式
     * @param mob 手机号码
     * @param valicode 验证码
     * @param MOULD_ID 模板id
     * @return
     */
    public  boolean send(String mob, String valicode,String MOULD_ID) {
        try {
            // 创建HttpClient实例
            HttpClient httpclient = new DefaultHttpClient();

            //构造一个post对象
            HttpPost httpPost = new HttpPost(SERVER_URL);

            String curTime=String.valueOf((new Date().getTime()/1000L));
            String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,valicode,curTime);
            //设置请求的header
            httpPost.addHeader("AppKey",APP_KEY);
            httpPost.addHeader("Nonce",valicode);
            httpPost.addHeader("CurTime",curTime);
            httpPost.addHeader("CheckSum",checkSum);
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

            //设置请求参数
            List<NameValuePair> nameValuePairs =new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("templateid",MOULD_ID));
            nameValuePairs.add(new BasicNameValuePair("mobiles","['" + mob + "']"));
            nameValuePairs.add(new BasicNameValuePair("params","['" + valicode + "']"));

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

            HttpResponse response = httpclient.execute(httpPost);
            String responseEntity= EntityUtils.toString(response.getEntity(), "utf-8");

            //判断是否发送成功，发送成功返回true
            String code = JSON.parseObject(responseEntity).getString("code");

            if("200".equals(code)){
                return true;
            }else{
                System.out.println(responseEntity);
                return false;
            }


        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        SendSMSForNetease sendSMSForNetease = new SendSMSForNetease();

        sendSMSForNetease.send("13458537224","888882",MOULD_ID);

//        Map<String,String> m = gson.fromJson(je, HashMap.class);
//        gson.toJson(xx);
//        gson.
//        System.out.println(m.get("code"));
//        System.out.println(jo);

    }

}
